//
//  QMySelfInformationVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/5.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QMySelfInformationVC.h"
#import "QMySelfInformationCell.h"
#import "QChangeNickNameVC.h"
#import "QForgetPassWordVC.h"
@interface QMySelfInformationVC ()<UITableViewDataSource, UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UITableView *tbView;

@end

@implementation QMySelfInformationVC{

    UIButton *_signoutButton;
}

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//设置状态栏
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];//设置状态栏
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
   
    [self createUI];
}

- (void)createUI{
    WS(wSelf);
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(wSelf.view).insets(UIEdgeInsetsZero);
    }];
    [self.tbView registerNib:[UINib nibWithNibName:@"QMySelfInformationCell" bundle:nil] forCellReuseIdentifier:@"QMySelfInformationCell"];
    
    _signoutButton = [QViewUtility createButtonWithFrame:CGRectMake(0, kScreenHeight-50, kScreenWidth, 50) font:17 title:@"退出登录" titleColor:[QColorUtility defaultRedColor] backgroundColor:[UIColor whiteColor]];
    [_signoutButton whiteStyle];
    [_signoutButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_signoutButton];
}

#pragma mark--退出登录
- (void)buttonAction{
    [QViewUtility showAlertChooseViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"确定退出登录?" leftTitle:@"取消" rightTitle:@"确定" type:1 myBlock:^(NSInteger tag) {
        if(tag == 1){
            [self requestData];
        }
    }];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QMySelfInformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QMySelfInformationCell" forIndexPath:indexPath];
    cell.indexPath = indexPath;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0 && indexPath.row == 0){
        return 60;
    }else{
       return 50;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0){
        if(indexPath.row == 0){//头像
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册中选取", nil];
            [actionSheet showInView:self.view];
        }else{//昵称
        
            QChangeNickNameVC *vc = [[QChangeNickNameVC alloc] init];
            vc.nickNameStr = [QUserInfo sharedInfo].loginModel.nickName;
            vc.backBlock = ^(){
                [_tbView reloadData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
    
    }else if (indexPath.section == 1){//手机
        return;
    }else if (indexPath.section == 2){//手机密码
        QForgetPassWordVC *vc = [[QForgetPassWordVC alloc] init];
        vc.title = @"修改密码";
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
#pragma mark - ActionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    if (buttonIndex == 0) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    } else if (buttonIndex == 1) {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
}

#pragma mark - ImagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = info[UIImagePickerControllerEditedImage];
    [self requestImageData:image];
}

#pragma mark - 上传头像
- (void)requestImageData:(UIImage *)image{
    NSString *url = [QParamService urlWithAction:@"myheadImg"];
    NSDictionary *param = [QParamService editHeadPic];
   [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] uploadFile:url params:param image:image completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
       QBaseModel *basModel = [QBaseModel objectFromDict:dict];
       if([basModel.result isEqualToString:@"success"]){
           [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"修改成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
           NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
           QMySelfInformationCell *cell = (QMySelfInformationCell *)[_tbView cellForRowAtIndexPath:indexPath];
           cell.headImageView.image = image;
           [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSignup object:nil];
       }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
       }
    }];
}

#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.scrollEnabled = NO;
    }
    return _tbView;
}

#pragma mark - 退出登录
- (void)requestData{
    NSString *url = [QParamService urlWithAction:@"loginOutUser"];
    NSDictionary *param = [QParamService signOut];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QDataUtility deleteLoginModel];//清除本地信息
            //[QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"退出成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSignOut object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

@end
